package com.jichangxiu.generator.action;

import cn.hutool.core.collection.ListUtil;
import com.jichangxiu.common.entity.vo.PageResult;
import com.jichangxiu.generator.entity.dto.add.AddGenTemplate;
import com.jichangxiu.generator.entity.dto.edit.EditGenTemplate;
import com.jichangxiu.generator.entity.dto.query.QueryGenTemplate;
import com.jichangxiu.generator.entity.vo.GenTemplateVo;
import com.jichangxiu.generator.service.GenTemplateService;
import com.jichangxiu.framework.action.BaseAction;
import com.jichangxiu.common.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * GenTemplateAction:
 *
 * @author JiChangXiu
 * @create 2024-06-30 13:31:07
 */
@Slf4j
@Rest("/generator/template")
@Tag(name = "生成模板控制器")
public class GenTemplateAction extends BaseAction {

    @Resource
    private GenTemplateService genTemplateService;

    /**
     * 新增生成模板
     *
     * @param addGenTemplate 新增生成模板视图对象
     * @return 是否成功
     */
    @Post
    @Limit
    @Repeat
    @Log(remark = "新增生成模板")
    @Operation(summary = "新增生成模板")
    @PreAuthorize("@jcx.hasPower('generator:template:add')")
    public String add(@RequestBody AddGenTemplate addGenTemplate) {
        return str(genTemplateService.add(addGenTemplate));
    }

    /**
     * 单（多）选删除生成模板
     *
     * @param ids 生成模板主键列表
     * @return 是否成功
     */
    @Delete
    @Limit
    @Repeat
    @Log(remark = "单（多）选删除生成模板")
    @Operation(summary = "单（多）选删除生成模板")
    @PreAuthorize("@jcx.hasPower('generator:template:del')")
    public String del(@RequestBody String[] ids) {
        return str(genTemplateService.del(ListUtil.toList(ids)));
    }

    /**
     * 修改生成模板
     *
     * @param editGenTemplate 修改生成模板视图对象
     * @return 是否成功
     */
    @Put
    @Limit
    @Repeat
    @Log(remark = "修改生成模板")
    @Operation(summary = "修改生成模板")
    @PreAuthorize("@jcx.hasPower('generator:template:edit')")
    public String edit(@RequestBody EditGenTemplate editGenTemplate) {
        return str(genTemplateService.edit(editGenTemplate));
    }

    /**
     * 根据主键查询生成模板
     *
     * @param id       生成模板主键
     * @param isExtend 是否扩展
     * @return 生成模板详情
     */
    @Get("/{id}")
    @Operation(summary = "根据主键查询生成模板")
    @PreAuthorize("@jcx.hasPower('generator:template:info')")
    public GenTemplateVo info(@PathVariable("id") @NotBlank(message = "主键不能为空") String id, @RequestParam(value = "isExtend", required = false, defaultValue = "false") Boolean isExtend) {
        return genTemplateService.info(id, isExtend);
    }

    /**
     * （可分页）查询生成模板列表
     *
     * @param queryGenTemplate 查询视图对象
     * @return （分页）生成模板列表
     */
    @Get("/lists")
    @Operation(summary = "（可分页）查询生成模板列表")
    @PreAuthorize("@jcx.hasPower('generator:template:lists')")
    public PageResult lists(@ParameterObject QueryGenTemplate queryGenTemplate) {
        return pageResult(genTemplateService.lists(queryGenTemplate));
    }

}